﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Simple.Service;
using Simple.Web.Framework.Infrastructure.Aop;
using Simple.Common.Pager;
using Simple.ViewModel;
using Simple.Web.Framework.Infrastructure;
using Simple.Common.EasyUICommon;
using Simple.Entity.Enums;
using Simple.Entity.Models;

namespace HBootStrap.Admin.Controllers
{
    public class MenuController : BaseController
    {
        private readonly MenuService _menuService;
        public MenuController(MenuService menuService)
        {
            _menuService = menuService;
        }

        [MenuViewFilter(MenuName = "菜单管理", ModularName = "基础信息管理", Sort = 3)]
        public ActionResult Index(MenuQueryModel queryModel)
        {
            var model = _menuService.GetPageList(queryModel);
            return View(model);
        }

        public ActionResult GetIconByActionType(int actionType)
        {
            var icon = ActionTypeHelper.GetDefaultIcon((ActionTypeEnum)actionType);
            return Content(icon);
        }

        [ActionViewFilter(ActionName = "添加模块", MenuName = "菜单管理", Sort = 1, ActionType = ActionTypeEnum.Add)]
        public ActionResult AddModular()
        {
            ModularDto dto = new ModularDto();
            dto.Icon = WebSiteConfig.DefaultIcon;
            dto.Sort = 1;
            dto.ModularName = "默认模块";
            return View(dto);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "添加模块", MenuName = "菜单管理", Sort = 1, ActionType = ActionTypeEnum.Add)]
        
        public ActionResult AddModular(ModularDto modularDto)
        {
            Sys_Menu sys_Menu = new Sys_Menu()
            {
                ActionName = "#",
                ActionType = ActionTypeEnum.None,
                ControllerName = "#",
                Icon = modularDto.Icon ?? WebSiteConfig.DefaultIcon,
                MenuName = modularDto.ModularName,
                MenuType = MenuTypeEnum.菜单模块,
                ParentId = WebSiteConfig.DefaultParentID,
                Sort = modularDto.Sort
            };

            var result = _menuService.AddModular(sys_Menu);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Menu");
            }
            return Json(result);
        }

        [HttpGet]
        public ActionResult EditModular(ModularDto modularDto)
        {
            return View(modularDto);
        }

        [HttpPost]
        
        public ActionResult PostEditModular(ModularDto modularDto)
        {
            var updateMenu = _menuService.FindById(modularDto.Id);
            updateMenu.MenuName = modularDto.ModularName;
            updateMenu.Sort = modularDto.Sort;
            updateMenu.Icon = modularDto.Icon;
            var result = _menuService.Update(updateMenu);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Menu");
            }
            return Json(result);
        }

        [ActionViewFilter(ActionName = "添加菜单", MenuName = "菜单管理", Sort = 2, ActionType = ActionTypeEnum.Add)]
        public ActionResult AddMenu(long? Id, int? menuType)
        {
            MenuDto model = new MenuDto();
            model.Sort = 0;
            model.Icon = WebSiteConfig.DefaultIcon;
            model.ActionType = ActionTypeEnum.None;
            if (menuType == (int)MenuTypeEnum.菜单模块)
                model.ParentId = Id;
            model.ModularMenuList = _cacheService.GetModularMenuList();
            return View(model);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "添加菜单", MenuName = "菜单管理", Sort = 2, ActionType = ActionTypeEnum.Edit)]
        
        public ActionResult AddMenu(MenuDto menuDto)
        {
            var model = menuDto.ToEntity();
            model.MenuType = MenuTypeEnum.普通菜单;
            var result = _menuService.AddMenu(model);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Menu");
            }
            return Json(result);
        }

        [HttpGet]
        public ActionResult EditMenu(MenuDto menuDto)
        {
            menuDto.ModularMenuList = _cacheService.GetModularMenuList(menuDto.ParentId);
            return View(menuDto);
        }

        [HttpPost]
        
        public ActionResult PostEditMenu(MenuDto menuDto)
        {
            var updateMenu = _menuService.FindById(menuDto.Id);
            updateMenu.MenuName = menuDto.MenuName;
            updateMenu.Sort = menuDto.Sort;
            updateMenu.Icon = menuDto.Icon;
            var result = _menuService.Update(updateMenu);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Menu");
            }
            return Json(result);
        }

        [ActionViewFilter(ActionName = "添加按钮", MenuName = "菜单管理", Sort = 3, ActionType = ActionTypeEnum.Add)]
        public ActionResult AddButton(long? Id, int? menuType)
        {
            MenuDto model = new MenuDto();
            model.Sort = 0;
            model.Icon = WebSiteConfig.DefaultIcon;
            model.ActionType = ActionTypeEnum.Add;
            if (menuType == (int)MenuTypeEnum.普通菜单)
                model.ParentId = Id;
            model.MenuList = _cacheService.GetMenuList();
            model.ActionTypeList = _cacheService.GetActionTypeList();
            return View(model);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "添加按钮", MenuName = "菜单管理", Sort = 3, ActionType = ActionTypeEnum.Add)]
        
        public ActionResult AddButton(MenuDto dto)
        {
            var model = dto.ToEntity();
            model.MenuType = MenuTypeEnum.菜单按钮;
            var result = _menuService.AddButton(model);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Menu");
            }
            return Json(result);
        }

        [HttpGet]
        public ActionResult EditButton(MenuDto menuDto)
        {
            menuDto.MenuList = _cacheService.GetMenuList(menuDto.ParentId);
            menuDto.ActionTypeList = _cacheService.GetActionTypeList((int)menuDto.ActionType);
            ViewData["ActionTypeVal"] = (int)menuDto.ActionType;
            return View(menuDto);
        }

        [HttpPost]
        
        public ActionResult PostEditButton(MenuDto menuDto)
        {
            var updateMenu = _menuService.FindById(menuDto.Id);
            updateMenu.MenuName = menuDto.MenuName;
            updateMenu.Sort = menuDto.Sort;
            updateMenu.Icon = menuDto.Icon;
            updateMenu.ActionType = menuDto.ActionType;
            var result = _menuService.Update(updateMenu);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Menu");
            }
            return Json(result);
        }

        [ActionViewFilter(ActionName = "编辑", MenuName = "菜单管理", Sort = 4, ActionType = ActionTypeEnum.Edit)]
        public ActionResult Edit(long id)
        {
            var model = _menuService.FindById(id);
            switch (model.MenuType)
            {
                case MenuTypeEnum.菜单模块: return RedirectToAction("EditModular", new ModularDto()
                {
                    Icon = model.Icon,
                    Sort = model.Sort,
                    ModularName = model.MenuName,
                    Id = model.Id
                });
                case MenuTypeEnum.普通菜单: return RedirectToAction("EditMenu", model.ToDto());
                case MenuTypeEnum.菜单按钮: return RedirectToAction("EditButton", model.ToDto());
                default: return View();
            }
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "编辑", MenuName = "菜单管理", Sort = 4, ActionType = ActionTypeEnum.Edit)]
        public ActionResult Edit(MenuDto dto)
        {
            var updateMenu = _menuService.FindById(dto.Id);
            updateMenu.Icon = dto.Icon;
            updateMenu.MenuName = dto.MenuName;
            updateMenu.ControllerName = dto.ControllerName;
            updateMenu.ActionName = dto.ActionName;
            updateMenu.Sort = dto.Sort;
            updateMenu.ActionType = dto.ActionType;

            var result = _menuService.Update(updateMenu);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Menu");
            }
            return Json(result);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "删除菜单", MenuName = "菜单管理", Sort = 5, ActionType = ActionTypeEnum.Delete)]
        public ActionResult Delete(long id)
        {
            var result = _menuService.Delete(id);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Menu");
            }
            return Json(result);
        }

    }
}
